"""
file: CheckoutPage.py
Author: 城下秋草
Desc: 结算流程Page对象
"""
from decimal import Decimal

from pom.pages.basePage import BasePage
from pom.util.custom_exception import ElementNotFound
from pom.util.logger import logger


class CheckoutPage(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self.page = page

    def checkout(self):
        checkout_btn = self.get_element("checkout","id")
        try:
            checkout_btn.click()
        except Exception as e:
            raise ElementNotFound("checkout")


    def input_info(self):
        first_text_box = self.get_element("firstName","test-id")
        last_text_box = self.get_element("lastName", "test-id")
        zipCode_text_box = self.get_element("postalCode", "test-id")
        continue_btn = self.get_element("continue","test-id")
        try:
            first_text_box.fill("秋草")
            last_text_box.fill("城下")
            zipCode_text_box.fill("000000")
            continue_btn.click()
        except Exception as e:
            raise ElementNotFound("输入信息元素识别错误！")

    def get_prices(self) -> list:
        product_list = self.get_element("inventory-item-price","test-id").element_handles()
        prices = [Decimal(product.text_content().replace("$","")) for product in product_list]
        return prices

    def get_total(self,tag):
        total_text = ""
        if tag == "item":
            total_text = self.get_element("subtotal-label","test-id").text_content()
        elif tag == "tax":
            total_text = self.get_element("tax-label", "test-id").text_content()
        elif tag == "total":
            total_text = self.get_element("total-label", "test-id").text_content()
        else:
            logger.error(f"输入类型错误:{tag}")
            raise ValueError
        total = total_text.split("$")[1]
        return Decimal(total)

    def finish(self):
        try:
            self.get_element("finish","id").click()
        except Exception as e:
            raise ElementNotFound("finish")

    def is_finished(self):
        if self.get_element("complete-header","test-id").is_visible():
            return True
        else:
            return False